package org.jetnuke.blocks;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IBlock;
import org.jetnuke.common.IBlockInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.PollDAO;
import org.jetnuke.dao.PollDataDAO;
import org.jetnuke.entities.Poll;
import org.jetnuke.entities.PollData;

public class Survey implements IBlock {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IBlock#serviceBlock(javax.servlet.http.HttpServletRequest,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IBlockInfo)
	 */
	public void serviceBlock(HttpServletRequest req, VelocityContext vc,
			IBlockInfo blockInfo, IUserInfo userInfo, IAdminInfo adminInfo)
			throws Exception {
		PollDAO pm=DataAccessDriver.getInst().newJPollDAO();
		Poll poll = pm.getLast();
			//pollPeer.doSelect(new Criteria().addAscendingOrderByColumn(
				//pollPeer.POLLID).setLimit(1));
		if (poll!=null) {
			//Poll poll = (Poll) ls.get(0);
			PollDataDAO pdm=DataAccessDriver.getInst().newPollDataDAO();
			List options =pdm.selectByPollIDOrderVoteID(poll.getPOLLID());
				//poll_dataPeer.doSelect(new Criteria().add(
					//poll_dataPeer.POLLID, poll.getPOLLID())
					//.addAscendingOrderByColumn(poll_dataPeer.VOTEID));
			Iterator it = options.iterator();
			int sum = 0;
			while (it.hasNext()) {
				PollData dt = (PollData) it.next();
				sum += dt.getOPTIONCOUNT();
			}
			vc.put("sum", new Integer(sum));
			vc.put("comments", new Integer(0));
			vc.put("poll", poll);
			vc.put("options", options);
		}
	}

}
